package org.springblade.modules.admin.task;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.TemplateMsgConstant;
import org.springblade.common.utils.message.domain.MessageParam;
import org.springblade.common.utils.message.domain.SendRequest;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.MaintainPlanContentExt;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.IMaintainPlanContentExtService;
import org.springblade.modules.admin.service.IMaintainScoreMsgService;
import org.springblade.modules.admin.service.WeChatDialogueService;
import org.springblade.modules.admin.vo.MapUserInfoDetailVO;
import org.springblade.modules.admin.vo.RescueOrderDetail;
import org.springblade.modules.admin.vo.plan.store.WxSendWeiXinMsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维修评分发送模板消息
 *
 * @author zcc
 * @version 1.0
 * @date 2025/3/5 9:51
 */
@Component
@Slf4j
public class PlanScoreJob {


	@Autowired
	private IMaintainPlanContentExtService maintainPlanContentExtService;

	@Autowired
	private WeChatDialogueService weChatDialogueService;

	@Autowired
	private IMaintainScoreMsgService maintainScoreMsgService;

	/**
	 * 定时任务：发送维修的评分结果
	 */
	@XxlJob(value = "sendWxScoreResult")
	public void sendWxScoreResult() {
		XxlJobHelper.handleSuccess("发送维修评分模板消息-开始执行");
		List<WxSendWeiXinMsgVo> scoreYesterdayList = maintainPlanContentExtService.getScoreYesterdayList();
		Map<Long, List<WxSendWeiXinMsgVo>> listMap = scoreYesterdayList.stream().collect(Collectors.groupingBy(WxSendWeiXinMsgVo::getUserId));
		List<Long> userIds = scoreYesterdayList.stream().map(WxSendWeiXinMsgVo::getUserId).collect(Collectors.toList());
		for (Long userId : userIds) {
			List<WxSendWeiXinMsgVo> wxSendWeiXinMsgVos = listMap.get(userId);
			WxSendWeiXinMsgVo wxSendWeiXinMsgVo = wxSendWeiXinMsgVos.get(0);
			String skipUrl = String.format("/subpages/workOrders/records?type=2");
			Map<String, String> templateDataMap = new HashMap<>(8);
			templateDataMap.put("first", "工单评价提醒");
			if (wxSendWeiXinMsgVos.size() == 1) {
				templateDataMap.put("keyword1", wxSendWeiXinMsgVo.getPlanId().toString());
				templateDataMap.put("keyword2", DateUtil.format(wxSendWeiXinMsgVo.getPlanDate(), "yyyy-MM-dd"));
				templateDataMap.put("keyword3", "维修");
				templateDataMap.put("keyword4", wxSendWeiXinMsgVo.getUserName());
				templateDataMap.put("keyword5", DateUtil.format(wxSendWeiXinMsgVo.getPlanEndTime(), "yyyy-MM-dd"));
			} else {
				templateDataMap.put("keyword1", wxSendWeiXinMsgVos.size() + "个");
				templateDataMap.put("keyword2", DateUtil.format(wxSendWeiXinMsgVo.getPlanDate(), "yyyy-MM-dd") + "~" + DateUtil.format(wxSendWeiXinMsgVos.get(wxSendWeiXinMsgVos.size()).getPlanDate(), "yyyy-MM-dd"));
				templateDataMap.put("keyword3", "维修");
				templateDataMap.put("keyword4", wxSendWeiXinMsgVo.getUserName());
				templateDataMap.put("keyword5", DateUtil.format(wxSendWeiXinMsgVo.getPlanEndTime(), "yyyy-MM-dd") + "~" + DateUtil.format(wxSendWeiXinMsgVos.get(wxSendWeiXinMsgVos.size()).getPlanEndTime(), "yyyy-MM-dd"));
			}
			templateDataMap.put("remark", "请及时登录梯无忧小程序执行维修任务!");
			templateDataMap.put("params", skipUrl);
			weChatDialogueService.sendMsg(wxSendWeiXinMsgVo.getOpenId(), TemplateMsgConstant.SCORE_WX_MESSAGE, templateDataMap);
		}

		XxlJobHelper.handleSuccess("发送维修评分模板消息-开始执行");
	}


	/**
	 * 发送保养不合格信息
	 */
	@XxlJob(value = "sendByStoreNoPass")
	public void sendByStoreNoPass() {
		XxlJobHelper.handleSuccess("发送保养不合格信息-开始执行");
		maintainScoreMsgService.sendByStoreNoPass();
		XxlJobHelper.handleSuccess("发送维修评分模板消息-执行结束");
	}


	/**
	 * 整改工单逾期 发送消息
	 */
	@XxlJob(value = "abarbeitungPlanTimeOut")
	public void abarbeitungPlanTimeOut() {
		XxlJobHelper.handleSuccess("整改工单逾期-开始执行");
		maintainScoreMsgService.abarbeitungPlanTimeOut();
		XxlJobHelper.handleSuccess("整改工单逾期-执行结束");
	}
}
